package com.lh.runmanage.controller;

import com.lh.runmanage.model.ColModel;
import com.lh.runmanage.model.InPutParam.ColInParam;
import com.lh.runmanage.model.InPutParam.MenuHistoryModel;
import com.lh.runmanage.model.MenuHistoryListModel;
import com.lh.runmanage.model.MenuHistoryTotalModel;
import com.lh.runmanage.model.ResultVO;
import com.lh.runmanage.service.ColService;
import com.lh.runmanage.service.MenuAuthorityService;
import com.lh.runmanage.unit.GetPropertiesClass;
import com.lh.runmanage.unit.ResultStruct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-12 18:15
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/colController")
@Api(value = "项目控制层", description = "系统运行态势平台管理端方法")
public class ColController {
    @Autowired
    ColService colService;
    @Autowired
    GetPropertiesClass getPropertiesClass;
    @Autowired
    MenuAuthorityService menuAuthorityService;

    /**
     * 得到单位菜单值，方法ID：SE20210712180630103832FC8313FB2
     *
     * @param projectId 表vou_col,字段名id:菜单功能ID
     * @return 一个菜单值
     */
    @ApiOperation(value = "根据项目ID，得到所有已设置菜单权重值,方法ID：SE20210712180630103832FC8313FB5", notes = "/colController/V1/selectColByProjectCode?projectId=projectId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", dataType = "String", required = true)
    })
    @PostMapping("/V1/selectColByProjectCode")
    public ResultVO selectColByProjectCode(@RequestParam(value = "projectId") String projectId) {
        ColInParam colInParam = new ColInParam();
        colInParam.setProjectCode(projectId);
        final List<ColModel> colModels = colService.selectColByProjectCode(colInParam);
        return ResultStruct.success(colModels);
    }

    /**
     * 得到单位菜单值，方法ID：SE20210712180630103832FC8313FB2
     *
     * @param menuCode  表vou_col,字段名id:菜单功能ID
     * @param projectId 表vou_col,字段名id:菜单功能ID
     * @return 一个菜单值
     */
    @ApiOperation(value = "得到单位菜单值[一个菜单值],方法ID：SE20210712180630103832FC8313FB2", notes = "/colController/V1/selectColById?id=id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuCode", value = "菜单功能ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "projectId", value = "项目ID", dataType = "String", required = true)
    })
    @PostMapping("/V1/selectColById")
    public ResultVO selectColById(@RequestParam(value = "menuCode") String menuCode
            , @RequestParam(value = "projectId") String projectId
    ) {
        ColInParam colInParam = new ColInParam();
        colInParam.setId(menuCode);
        colInParam.setProjectCode(projectId);
        ColModel colModel = colService.selectColById(colInParam);
        if (colModel == null) {
            colModel = new ColModel();
            colModel.setCaliCaption("一般");
            colModel.setCalibration(0);
            colModel.setWeight(BigDecimal.valueOf(0));
            colModel.setProjectCode(projectId);
            colModel.setId(menuCode);
        }

        MenuHistoryTotalModel menuHistoryListModel = new MenuHistoryTotalModel();
        menuHistoryListModel.setId(colModel.getId());
        menuHistoryListModel.setCalibration(colModel.getCalibration());
        menuHistoryListModel.setProjectCode(colModel.getProjectCode());
        menuHistoryListModel.setCalibration(colModel.getCalibration());
        menuHistoryListModel.setWeight(colModel.getWeight());
        menuHistoryListModel.setCaliCaption(colModel.getCaliCaption());

        MenuHistoryModel menuHistoryModel = new MenuHistoryModel();
        menuHistoryModel.setProjectCode(projectId);
        menuHistoryModel.setMenuCode(menuCode);
        final BigDecimal bigDecimal = colService.selectColTotalWeightByProjectId(projectId);
        final List<MenuHistoryListModel> menuHistoryListModels = menuAuthorityService.selectHistoryCount(menuHistoryModel);
        menuHistoryListModel.setTotalWeigth(bigDecimal);
        menuHistoryListModel.setMenuHistoryListModels(menuHistoryListModels);
        return ResultStruct.success(menuHistoryListModel);
    }

    /**
     * 修改一个菜单值，方法ID：UP2021071218185988076712B6B52EA
     *
     * @param menuCode    菜单功能ID
     * @param caliCaption 标定级别（初始：10、低：30、一般：50、较高：80、高：100）
     * @param calibration 标定数（大于0）
     * @param weight      分值（0--100）
     * @return 影响条数
     */
    @ApiOperation(value = "修改一个菜单值[影响条数],方法ID：UP2021071218185988076712B6B52EA", notes = "/colController/V1/updateColById?id=id&caliCaption=caliCaption&calibration=calibration&weight=weight")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuCode", value = "菜单功能ID, 长度：50", dataType = "String", required = true)
            , @ApiImplicitParam(name = "projectId", value = "菜单功能ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "caliCaption", value = "标定级别（初始：10、低：30、一般：50、较高：80、高：100）, 长度：10", dataType = "String", required = true)
            , @ApiImplicitParam(name = "calibration", value = "标定数（大于0）, 长度：3", dataType = "String", required = true)
            , @ApiImplicitParam(name = "weight", value = "权重", dataType = "String", required = true)
    })
    @PostMapping("/V1/updateColById")
    public ResultVO updateColById(@RequestParam(value = "menuCode") String menuCode
            , @RequestParam(value = "projectId") String projectId
            , @RequestParam(value = "caliCaption") String caliCaption
            , @RequestParam(value = "calibration") int calibration
            , @RequestParam(value = "weight") BigDecimal weight) throws UnsupportedEncodingException {
        if (caliCaption != null) {
            caliCaption = caliCaption.trim();
            if (caliCaption.getBytes("gbk").length > 10) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：10", "caliCaption"), ResultVO.class, null);
            }
        }

        if (calibration <= 0)
            return ResultStruct.error(String.format("字段【%s】，不能小于等于0", "caliCaption"), ResultVO.class, null);
        if (weight.compareTo(BigDecimal.valueOf(0)) < 0)
            return ResultStruct.error(String.format("字段【%s】，不能小于等于0", "weight"), ResultVO.class, null);

        boolean resultSign = false;
        BigDecimal totalCount = BigDecimal.valueOf(0);
        ColInParam colInParam = new ColInParam();
        colInParam.setId(menuCode);
        colInParam.setProjectCode(projectId);
        colInParam.setCaliCaption(caliCaption);
        if (calibration > -128)
            colInParam.setCalibration(calibration);
        colInParam.setWeight(weight);
        int resultCount = colService.updateColById(colInParam);
        if (resultCount > 0) {
            resultSign = true;
        } else {
            resultCount = colService.insertCol(colInParam);
            if (resultCount > 0) {
                resultSign = true;
            }
        }
        if (resultSign)
            totalCount = colService.selectColTotalWeightByProjectId(projectId);
        return resultSign ? ResultStruct.success(String.format("更新成功，本项目权重小计为：%.2f", totalCount))
                : ResultStruct.error("更新失败", ResultVO.class, null);
    }

    /**
     * 删除一个菜单值，方法ID：DE20210712181900137F9E07808AB36
     *
     * @param menuCode 菜单功能ID
     * @return 影响条数
     */
    @ApiOperation(value = "删除一个菜单值[影响条数],方法ID：DE20210712181900137F9E07808AB36", notes = "/colController/V1/deleteColById?id=id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuCode", value = "菜单功能ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "projectId", value = "菜单功能ID", dataType = "String", required = true)
    })
    @PostMapping("/V1/deleteColById")
    public int deleteColById(@RequestParam(value = "menuCode") String menuCode
            , @RequestParam(value = "projectId") String projectId) {
        ColInParam colInParam = new ColInParam();
        colInParam.setId(menuCode);
        colInParam.setProjectCode(projectId);
        return colService.deleteColById(colInParam);
    }
}